package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.hosp.service.IOrderInfoService;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author Fxq
 * @since 2022-04-07
 */
@RestController
@RequestMapping("/api/hosp/order-info")
@Api(value = "OrderInfoController",tags = "订单管理")
public class OrderInfoController {

    @Resource
    private IOrderInfoService orderService;

    @ApiOperation(value = "创建订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result submitOrder(
            @ApiParam(name = "scheduleId", value = "排班id", required = true)
            @PathVariable String scheduleId,
            @ApiParam(name = "patientId", value = "就诊人id", required = true)
            @PathVariable Long patientId) {
        return new Result(orderService.saveOrder(scheduleId, patientId));
    }

    @GetMapping("auth/getOrder/{orderId}")
    public Result list(@PathVariable String orderId) {
        OrderInfo orderInfo=orderService.getOrderInfo(orderId);
        orderInfo.getParam().put("orderStatusString",OrderStatusEnum.getStatusNameByStatus(orderInfo.getOrderStatus()));
        return new Result(orderInfo);
    }

    /**
     * 订单列表（条件查询带分页）
     */
    @GetMapping("auth/{page}/{limit}")
    public Result list(@PathVariable Long page, @PathVariable Long limit,
                       OrderQueryVo orderQueryVo, HttpServletRequest request) {
        //设置当前用户id
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> pageParam = new Page<>(page,limit);
        IPage<OrderInfo> pageModel = orderService.selectPage(pageParam,orderQueryVo);
        return new Result(pageModel);
    }

    @ApiOperation(value = "获取订单所有状态")
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        return new Result(OrderStatusEnum.getStatusList());
    }

    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result cancelOrder(@ApiParam(name = "orderId", value = "订单id", required = true) @PathVariable("orderId") Long orderId) {
        return new Result(orderService.cancelOrder(orderId));
    }




}

